package org.jcnc.staffingsystem.stage;

import atlantafx.base.controls.Message;
import atlantafx.base.theme.Styles;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.jcnc.staffingsystem.db.EmployeeManager;
import org.jcnc.staffingsystem.module.ButtonHBox;
import org.jcnc.staffingsystem.module.ChoiceBox;
import org.jcnc.staffingsystem.module.PasswordBox;
import org.jcnc.staffingsystem.module.TextBox;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import static org.jcnc.staffingsystem.InitUI.initUI;
import static org.jcnc.staffingsystem.list.OptionsManager.getPositionOptions;

public class RegisterScene extends Stage {
    TextBox accountBox;
    public RegisterScene() {
        StackPane stackPane = new StackPane();

        VBox mainBox = new VBox(30);
        accountBox = new TextBox("输入账户");
        PasswordBox passwordBox = new PasswordBox("输入密码");
        PasswordBox againPasswordBox = new PasswordBox("再次输入");

        ChoiceBox choiceBox = new ChoiceBox("选择权限", getPositionOptions(), "");
        mainBox.setAlignment(Pos.CENTER);

        ButtonHBox buttonHBox = new ButtonHBox("取消", "确认");
        buttonHBox.setOneAction(actionEvent -> {
            this.close();
            initUI(this);
        });
        buttonHBox.setTwoAction(actionEvent -> {
            // 获取用户输入的账户、密码和权限
            String account = accountBox.getText();
            String password = passwordBox.getText();
            String againPassword = againPasswordBox.getText();
            String selectedPermission = choiceBox.getValue();

            if (account.isEmpty() || password.isEmpty() || againPassword.isEmpty() || selectedPermission.isEmpty()) {
                Message warning = new Message(
                        "Warning",
                        "输入空"
                );
                warning.getStyleClass().add(Styles.DANGER);
                warning.setMaxSize(200, 50);
                stackPane.getChildren().add(warning);

            } else if (!password.equals(againPassword)) {
                System.out.println("两次输入的密码不一致");
                Message warning = new Message(
                        "Warning",
                        "两次输入的密码不一致"
                );
                warning.getStyleClass().add(Styles.DANGER);
                warning.setMaxSize(200, 50);
                stackPane.getChildren().add(warning);

            } else {
                try {
                    // 输入合法，发送注册信息
                    sendRegistrationInfo(account, password, selectedPermission);

                    EmployeeManager employeeManager = new EmployeeManager();
                    employeeManager.insertEmployee("", "", "", "", "",
                            "", "", accountBox.getText());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        mainBox.getChildren().addAll(accountBox, passwordBox, againPasswordBox, choiceBox, buttonHBox);

        stackPane.getChildren().addAll(mainBox);
        stackPane.setAlignment(Pos.BOTTOM_RIGHT); // 将栈布局设置为右下角对齐

        Scene scene = new Scene(stackPane, 1000, 600);

        this.setScene(scene);
        this.setTitle("注册页面");
        this.show();
    }

    private void sendRegistrationInfo(String account, String password, String selectedPermission) throws Exception {
        try {
            URL url = new URL("http://localhost:8000/register");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);

            String data = "username=" + account + "&password=" + password + "&permission=" + selectedPermission;
            byte[] postData = data.getBytes(StandardCharsets.UTF_8);
            int postDataLength = postData.length;
            conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
            conn.getOutputStream().write(postData);

            int responseCode = conn.getResponseCode();
            System.out.println("Register Response Code: " + responseCode);

            if (responseCode==200){
                this.close();
                initUI(this);
            }

            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
